//
//  XYCustomVideoView.m
//  NemoSDKDemo
//
//  Created by 高超 on 2020/8/1.
//  Copyright © 2020 JackYang. All rights reserved.
//

#import "XYCustomVideoView.h"

@interface XYCustomVideoView ()

@property (nonatomic, strong) UIView *micAndNameView;

@end

@implementation XYCustomVideoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"onhold_normal_bg.jpg"]];
        _bgImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self addSubview:_bgImageView];
        
        _micAndNameView = [[UIView alloc] init];
        _micAndNameView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        _micAndNameView.hidden = YES;
        [self addSubview:_micAndNameView];
        
        _micImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"calling_unmute_mic"] highlightedImage:[UIImage imageNamed:@"calling_mute_mic"]];
        [_micAndNameView addSubview:_micImgView];
        
        _avatarImgView = [[UIImageView alloc] init];
        _avatarImgView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_avatarImgView];
        
        _fullScreenNameLabel = [[UILabel alloc] init];
        _fullScreenNameLabel.font = [UIFont systemFontOfSize:14];
        _fullScreenNameLabel.textColor = [UIColor whiteColor];
        _fullScreenNameLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_fullScreenNameLabel];
        
        _displayNameLabel = [[UILabel alloc] init];
        _displayNameLabel.font = [UIFont systemFontOfSize:10];
        _displayNameLabel.textColor = [UIColor whiteColor];
        _displayNameLabel.textAlignment = NSTextAlignmentCenter;
        [_micAndNameView addSubview:_displayNameLabel];
        
        _alertMsgLabel = [[UILabel alloc] init];
        _alertMsgLabel.textColor = [UIColor colorWithWhite:1 alpha:0.6];
        _alertMsgLabel.textAlignment = NSTextAlignmentCenter;
        _alertMsgLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_alertMsgLabel];
        
        _indicator = [[XYActivityIndicator alloc] initWithImage:[UIImage imageNamed:@"waiting_loading"]];
        [self addSubview:_indicator];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _bgImageView.frame = self.bounds;
    CGSize size = [_layout.remoteName boundingRectWithSize:CGSizeMake(0, 12) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} context:nil].size;
    if (CGSizeEqualToSize(self.bounds.size, kScreen)) {
        _micAndNameView.frame = CGRectMake(10, [UIApplication sharedApplication].statusBarFrame.size.height+10, size.width+20, 18);
    }else {
        _micAndNameView.frame = CGRectMake(0, self.xy_height-18, size.width+20, 18);
    }
    _displayNameLabel.frame = CGRectMake(17, 3, size.width, 12);
    _micImgView.frame = CGRectMake(3, 3, 11, 12);
    _indicator.frame = CGRectMake(0, 0, 20, 20);
    _indicator.center = CGPointMake(self.xy_width/2, self.xy_height/2);
    _alertMsgLabel.frame = CGRectMake(0, 0, self.xy_width, 20);
    _alertMsgLabel.center = CGPointMake(self.xy_width/2, self.xy_height/2);
    
    //展示名称
    _displayNameLabel.text = _layout.remoteName;
    
    if (self.isFullScreen && self.layout.layoutState != NemoStreamState_Received) {
        _avatarImgView.hidden = NO;
        _fullScreenNameLabel.hidden = NO;
        _avatarImgView.frame = CGRectMake((self.xy_width-64)/2, (self.xy_height-64)/2-35, 64, 64);
        _avatarImgView.layer.cornerRadius = 32.f;
        _avatarImgView.layer.masksToBounds = YES;
        _avatarImgView.image = [UIImage imageNamed:@"xyaddressbook_people"];
        
        _fullScreenNameLabel.frame = CGRectMake(0, CGRectGetMaxY(_avatarImgView.frame)+12, self.xy_width, 15);
//        _fullScreenNameLabel.text = _layout.remoteName;
        _fullScreenNameLabel.text = @"";

        
        _alertMsgLabel.xy_y = CGRectGetMaxY(_fullScreenNameLabel.frame)+5;
    }else {
        _avatarImgView.hidden = YES;
        _fullScreenNameLabel.hidden = YES;
    }
}

- (void)setLayout:(NemoLayout *)layout {
    _layout = layout;
    
    _bgImageView.hidden = layout.layoutState==NemoStreamState_Received;
    _avatarImgView.hidden = !self.isFullScreen || layout.layoutState==NemoStreamState_Received;
    _fullScreenNameLabel.hidden = !self.isFullScreen || layout.layoutState==NemoStreamState_Received;
    
    if (!self.isLocalView) {
        //麦克风静音
        _micImgView.highlighted = layout.isAudioMute;
        
        if (layout.layoutState == NemoStreamState_Observing ||
            layout.layoutState == NemoStreamState_Idle ||
            layout.layoutState == NemoStreamState_NoBandwidth ||
            layout.layoutState == NemoStreamState_NoDecoder ||
            layout.layoutState == NemoStreamState_Mute) {
            _alertMsgLabel.hidden = NO;
            _indicator.hidden = YES;
            _alertMsgLabel.text = @"视频暂停";
            [_indicator stopAnimating];
        }else if (layout.layoutState == NemoStreamState_Requesting) {
            _alertMsgLabel.hidden = YES;
            [_indicator startAnimating];
        }else if (layout.layoutState == NemoStreamState_Telephone ||
                  layout.layoutState == NemoStreamState_AudioOnly ||
                  layout.layoutState == NemoStreamState_ReceivedAudioOnly) {
            _alertMsgLabel.hidden = NO;
            [_indicator stopAnimating];
            if (layout.layoutState == NemoStreamState_Telephone) {
                if (self.isFullScreen) {
                     _alertMsgLabel.text = @"通话中";
                }else {
                     _alertMsgLabel.text = @"电话语音";
                }
            }else {
                _alertMsgLabel.text = @"语音通话中";
            }
        }else if (layout.layoutState == NemoStreamState_Received) {
            _alertMsgLabel.hidden = YES;
            [_indicator stopAnimating];
        }
    }else {
        [_indicator stopAnimating];
        //麦克风静音
        _micImgView.highlighted = layout.isAudioMute;
        
        if (layout.layoutState == NemoStreamState_Received) {
            _alertMsgLabel.hidden = YES;
        }else if (layout.layoutState == NemoStreamState_Mute) {
            _alertMsgLabel.hidden = NO;
            _alertMsgLabel.text = @"视频已关闭";
        }else if (layout.layoutState == NemoStreamState_AudioOnly) {
            _alertMsgLabel.hidden = NO;
            _alertMsgLabel.text = @"语音通话中";
        }
    }
    [self layoutSubviews];
}

- (void)fullScreen:(BOOL)landScape {
    self.isFullScreen = YES;
    if (landScape || self.isLocalView) {
        self.frame = CGRectMake(0, 0, kScreen.width, kScreen.height);
    }else {
        CGSize size = [self _videoSize];
        if (self.layout.videoWidth == 0 || self.layout.videoHeight == 0) {
            if ((self.xy_width>80 || self.xy_height>120) && !self.layout.isContent) {
                if (self.landScape) {
                    size.width = self.xy_height;
                    size.height = self.xy_width;
                    if (size.width < [UIScreen mainScreen].bounds.size.width &&
                        size.height < [UIScreen mainScreen].bounds.size.height) {
                        size.width = self.xy_width;
                        size.height = self.xy_height;
                    }
                }else {
                    size = self.xy_size;
                }
                self.frame = CGRectMake(self.xy_x, self.xy_y, size.width, size.height);
            }else {
                self.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height);
            }
        }else if (self.layout.videoWidth > self.layout.videoHeight) {
            self.frame = CGRectMake(0, (kScreen.height-size.height)/2, size.width, size.height);
        }else {
            self.frame = CGRectMake((kScreen.width-size.width)/2, 0, size.width, size.height);
        }
    }
    self.xy_centerX = kScreen.width/2;
    self.xy_centerY = kScreen.height/2;
    self.layer.borderWidth = 0;
    self.layer.borderColor = [UIColor clearColor].CGColor;
    [self.superview sendSubviewToBack:self];
    
    self.landScape = landScape;
}

- (CGSize)_videoSize {
    if (!self.layout.videoWidth || !self.layout.videoHeight) {
        return CGSizeZero;
    }
    if (self.layout.videoWidth > self.layout.videoHeight) {
        return CGSizeMake(kScreen.width, ((CGFloat)self.layout.videoHeight/self.layout.videoWidth)*kScreen.width);
    }else {
        return CGSizeMake((CGFloat)self.layout.videoWidth/self.layout.videoHeight*kScreen.height, kScreen.height);
    }
}

- (void)moveToPoint:(CGPoint)point isFixSize:(BOOL)isFixSize isTop:(BOOL)isTop landScape:(BOOL)landScape {
    self.isFullScreen = NO;
    CGSize size = CGSizeZero;
    if (isFixSize) {
        NemoLayout *layout = self.layout;
        if (landScape) {
            size = CGSizeMake(120, 80);
        }else {
            if (layout && layout.videoHeight && layout.videoHeight) {
                if (layout.videoWidth > layout.videoHeight) {
                    point.x = kScreen.width - 125;
                    size = CGSizeMake(120, 80);
                }else {
                    size = CGSizeMake(80, 120);
                }
            }else {
                size = CGSizeMake(80, 120);
            }
        }
        self.layer.borderWidth = 1.f;
        self.layer.borderColor = [UIColor whiteColor].CGColor;
    }else {
        if (isTop) {
            size = CGSizeMake(kScreen.width, BIGHEIGHT);
        }else {
            size = CGSizeMake(kScreen.width/2, SMALLHEIGHT);
        }
        self.layer.borderWidth = 0;
        self.layer.borderColor = [UIColor clearColor].CGColor;
    }
    self.frame = CGRectMake(point.x, point.y, size.width, size.height);
}

- (void)updateFrame:(CGRect)frame {
    self.frame = frame;
    self.layer.borderWidth = 0;
    self.layer.borderColor = [UIColor clearColor].CGColor;
}

@end
